<?php
	defined('_JEXEC') or die('Restricted access');
	require_once(CUTEENGINE_BASE_FIELD_PATH."base_field_class.php");
    
	//
    // t_field_memory_container
    //
	class t_field_memory_container	extends t_base_field
	{
        // property declaration
		// public
		// C
		public $cookie_name = '';
		
		const language_position_label = 1;
		const language_position_link = 2;
		const language_position_link_title_not_started = 3;
		const language_position_link_title_started = 4;
		const language_position_last = 5;
		
		// P
		const proper_properties_position_per_list_viewing_type = 1;
        const proper_properties_position_per_report_query = 2;
		const proper_properties_position_per_report_query_parameters_names = 3; 
        const proper_properties_position_per_add_item_query = 4;
		const proper_properties_position_per_add_item_query_parameters_names = 5; 
        //const proper_properties_position_per_get_list_query = 6;
		//const proper_properties_position_per_get_list_query_parameters_names = 7; 
		const proper_properties_position_per_component_id_to_get_list = 6;
        const proper_properties_position_per_use_custom_language = 7;
        const proper_properties_position_last = 8;        
        
		//  L
		const list_viewing_type_store_into_db = 1; // TO IMPLEMENT
		//const list_viewing_type_from_get_list_action_using_cookie = 2; // TO IMPLEMENT
		//const list_viewing_type_from_get_list_action_using_db = 3; // TO IMPLEMENT
		// M
		const memory_lists_key_param = 'memory_lists_key_param';


		// property declaration
		// protected	
		// L
		protected $label_tag = null;
		protected $count_tag = null;
		protected $link_tag = null;
		
        // method declaration
    	// protected
		// C
		protected function complete_field_tags($a_link_text,$a_count_text,$a_label_text)
        {			
			// label tag
			$this->label_tag->set_text_to_include(
                $a_label_text);
			if ($a_label_text == '')
			{
				// label tag
				$this->count_tag->set_text_to_include(
					'');
				// link tag
				$this->link_tag->set_text_to_include(
					$a_count_text);
			}
			else
			{
				// label tag
				$this->count_tag->set_text_to_include(
					$a_count_text);
				// link tag
				$this->link_tag->set_text_to_include(
					$a_link_text);
			}
			
			if ($this->is_started())		
			{
				$local_title = $this->lang[self::language_position_link_title_started];
			}
			else
			{
				$local_title = $this->lang[self::language_position_link_title_not_started];
			}
			//if (trim($local_title) != '')
			$this->link_tag->add_property('title', $local_title);	
			


		}	
        // G
		protected function get_action_name($a_prefix)
        {
            return $a_prefix . "_" . $this->name;
        } 
		protected function get_field_event_script($a_event_type,$a_argouments)
        {			
			//echo "a_event_type:$a_event_type<BR>";
			$local_return = '';
			
			switch ($a_event_type) 
            {
                case t_base_field::field_event_type_on_insert:
					//echo "this->main_tag->name: $this->main_tag->name<br>";
					//$this->label_tag->add_property("onclick",t_cuteengine::javascript_instance_name.".".$this->name.".".$this->get_event_name($a_event_type)."($a_argouments)");
					
				break;                
			}
			return $local_return;
		}
                
        protected function get_language_js_array()
        {
            return '["'.implode('","',$this->lang).'"]';
        }    
				
					
static function get_help_proper_property($a_index)
		{
			$local_return = 'NO HELP AVAILABLE';
			/*switch ($a_index) 
            {
                case self::proper_properties_position_:
					$local_return = '';
				break;                
			}*/
			return $local_return;
		}
        static function get_default_proper_property($a_index)
		{
			$local_return = '';
			/*switch ($a_index) 
            {
                case self::proper_properties_position_:
					$local_return = 'TRUE';
				break;                
			}*/
			return $local_return;
		}
		protected function get_proper_properties_position_last()      
		{
            return self::proper_properties_position_last;
        }
		// G
		protected function get_add_item()
        {
            $local_return = trim($this->proper_properties_array[self::proper_properties_position_per_add_item_query]);
			if ($local_return == '')
			{
				$local_return = "call ce_insert_memory_lists_items(:memory_lists_items_item_id:, :component_id:,  '".self::memory_lists_key_param."' )";
			}
			return $local_return;
        }		
		protected function get_add_item_parameters()
        {
			$local_return = '["'.str_replace(t_strings_utility_namespace::separator_level_2, '","', substr($this->proper_properties_array[self::proper_properties_position_per_add_item_query_parameters_names],1,-1)).'"]';;
			if (trim($this->proper_properties_array[self::proper_properties_position_per_add_item_query_parameters_names]) == '')
			{
				$local_return = '["memory_lists_items_item_id","component_id"]';
			}
			return $local_return;
        }
		protected function get_system_cookie_value()
        {
			if ($this->is_started())		
			{
				return $_COOKIE[$this->cookie_name];
			}
			else
				return '';
		}		
		protected function get_report_query()
        {
            $local_return = trim($this->proper_properties_array[self::proper_properties_position_per_report_query]);
			if ($local_return == '')
			{
				$local_return = "SELECT COUNT(`memory_lists_items_item_id`) AS count ".
					"FROM  `memory_lists_items` ".
					"INNER JOIN memory_lists ON memory_lists_items.memory_lists_id = memory_lists.memory_lists_id ".
					"WHERE memory_lists_key =  '".self::memory_lists_key_param."'";
			}
			return $local_return;
        }	
		protected function get_report_parameters()
        {
			$local_return = '["'.str_replace(t_strings_utility_namespace::separator_level_2, '","', substr($this->proper_properties_array[self::proper_properties_position_per_report_query_parameters_names],1,-1)).'"]';;
			if (trim($this->proper_properties_array[self::proper_properties_position_per_add_item_query_parameters_names]) == '')
			{
				$local_return = '[]';
			}
			return $local_return;
        }
		
		// I
		protected function is_started()
        {
            return ((array_key_exists($this->cookie_name,$_COOKIE)));
        }
		//S
		protected function set_system_cookie_value($a_key_value)
        {
			if (! $this->is_started())		
			{
				
				if (setcookie($this->cookie_name, $a_key_value))
					echo "setcookie OK<br> - $a_key_value";
				else
					echo "setcookie ERR<br> - $a_key_value";
				
			}
			else
				echo "COOKIE ALREADY SET<BR>";
			
		}
        // method declaration
    	// public
        public function __construct($a_parent_part,$a_common_properties_str, $a_proper_properties_str) 
		{
            
			
			global $g_ambient_namespace;
            parent::__construct($a_parent_part,$a_common_properties_str, $a_proper_properties_str);
            // LANGUAGES
			//echo "this->proper_properties_array[self::proper_properties_position_per_use_custom_language]: ".$this->proper_properties_array[self::proper_properties_position_per_use_custom_language]."<br>";
            if (strtoupper($this->proper_properties_array[self::proper_properties_position_per_use_custom_language]) == 'TRUE')
                $g_ambient_namespace->load_custom_language($this,$this->name);            
			// query report
            $this->parent_part->add_action_for_field_uses_as_included('["none","t_action_execute_query","'.$this->get_action_name('get_report').'",["'.$this->name.'"],"'.
				$this->get_report_query().'",'.$this->get_report_parameters().',"FALSE","FALSE"]');
            // query report_from_event
            $this->parent_part->add_action_for_field_uses_as_included('["none","t_action_execute_query","'.$this->get_action_name('get_report_from_event').'",["'.$this->name.'"],"'.
				$this->get_report_query().'",'.$this->get_report_parameters().',"FALSE","TRUE"]');
			//echo "get_report -> ". md5($this->get_action_name('get_report'))	. "<br>";
			//echo "get_report_from_event -> ".$this->get_action_name('get_report_from_event'). "<br>";
            // add item    
            $this->parent_part->add_action_for_field_uses_as_included('["none","t_action_execute_query","'.$this->get_action_name('add_item').'",["'.$this->name.'"],"'.
				$this->get_add_item().'",'.$this->get_add_item_parameters().',"FALSE","TRUE"]');
			//echo "add_item -> ".$this->get_action_name('add_item'). " - " . md5($this->get_action_name('add_item'))	. "<br>";
            // query list    
            //$local_params = '["'.str_replace(t_strings_utility_namespace::separator_level_2, '","', substr($this->proper_properties_array[self::proper_properties_position_per_get_list_query_parameters_names],1,-1)).'"]';
            //$this->parent_part->add_action_for_field_uses_as_included('["none","t_action_execute_query","'.$this->get_action_name('get_list').'",["'.$this->name.'"],"'.
				//$this->proper_properties_array[self::proper_properties_position_per_get_list_query].'",'.$local_params.',"FALSE","TRUE"]');
			$this->cookie_name = md5($this->name);
		}
		// A
		public function apply_result_script($a_query_parameter,$a_current_row_no,$a_rows_count,$a_arguments)
        {
			global $g_cuteengine;
   			$local_action = $a_arguments[0];
            $local_action_name = $local_action->get_action_name();

			//echo "local_action_name: $local_action_name<br>";
            $local_result = $a_arguments[1];
           // print_r($local_result);
            if ($local_action_name == $this->get_action_name('get_report'))
			{
				$local_link_text = $this->lang[self::language_position_link];
				$local_count_text = $a_query_parameter->sql_values[0];
				$local_label_text = $this->lang[self::language_position_label];
				$this->complete_field_tags($local_link_text,$local_count_text,
					$local_label_text);
			}
            else if ($local_action_name == $this->get_action_name('get_report_from_event'))
			{
				$local_label_text = $this->lang[self::language_position_label];
				
				if ($this->lang[self::language_position_link] == '')
				{
					$local_count_text = '';
					$local_link_text = $a_query_parameter->sql_values[0];
				}
				else
				{
					$local_count_text = $a_query_parameter->sql_values[0];
					$local_link_text = $this->lang[self::language_position_label];
				}
				if ($this->is_started())		
				{
					$local_title = $this->lang[self::language_position_link_title_started];
				}
				else
				{
					$local_title = $this->lang[self::language_position_link_title_not_started];
				}
				
				$local_result->add_function_result(
					"","","","window.cuteengine.html_tag(window.cuteengine.".$this->name.".link_tag).innerHTML = '$local_link_text';".
	"window.cuteengine.html_tag(window.cuteengine.".$this->name.".link_tag).title = '$local_title';".					
	"window.cuteengine.html_tag(window.cuteengine.".$this->name.".count_tag).innerHTML = '$local_count_text';".				
	"window.cuteengine.html_tag(window.cuteengine.".$this->name.".label_tag).innerHTML = '$local_label_text';");    
			}
			else if ($local_action_name == $this->get_action_name('add_item'))
			{
				if ($a_query_parameter->sql_values[0] != '')  
				{
					$local_link_text = $this->lang[self::language_position_link];
					//$local_label_text = $this->lang[self::language_position_label] . "1000";
					$this->set_system_cookie_value($a_query_parameter->sql_values[0]);
					//

					$local_result->add_function_result(
						"","","",
		t_cuteengine::javascript_instance_name.".".$this->name.".".$this->get_event_name(t_base_field::field_event_type_on_selected)."();".
		t_cuteengine::javascript_instance_name.".html_tag(".t_cuteengine::javascript_instance_name.".".$this->name.".link_tag).href = '".$this->get_link_value($a_query_parameter->sql_values[0])."';".
		t_cuteengine::javascript_instance_name.".".$this->name.".last_tag_name_in_action = ".t_cuteengine::javascript_instance_name.".".$this->name.".current_tag_name_in_action;".
		t_cuteengine::javascript_instance_name.".".$this->name.".current_tag_name_in_action = '';");    
				}
				else    
				{
					$this->apply_error_result_script($a_query_parameter,$a_current_row_no,$a_rows_count,$a_arguments);
				}
			
			}
		}
		public function apply_error_result_script($a_query_parameter,$a_current_row_no,$a_rows_count,$a_arguments)
        {
        
   			$local_action = $a_arguments[0];
            $local_action_name = $local_action->get_action_name();

			//echo "local_action_name: $local_action_name<br>";
            $local_result = $a_arguments[1];
           // print_r($local_result);
            if ($local_action_name == $this->get_action_name('get_report'))
			{
				$local_link_text = $this->lang[self::language_position_link];
				$local_label_text = $a_query_parameter->get_last_error();
					
				$this->complete_field_tags($local_link_text,$this->count_tag->text_to_include,
					$local_label_text);
			}
            else if ($local_action_name == $this->get_action_name('get_report_from_event'))
			{
				$local_link_text = $this->lang[self::language_position_link];
				$local_label_text = $a_query_parameter->get_last_error();
				//echo "local_label_text: $local_label_text<br>";
				/*$local_result->add_function_result(
					"","","","window.cuteengine.html_tag(window.cuteengine.".$this->name.".link_tag).innerHTML = '$local_link_text';".
		"window.cuteengine.html_tag(window.cuteengine.".$this->name.".label_tag).innerHTML = '$local_label_text';");    */
			}
			else if ($local_action_name == $this->get_action_name('add_item'))
			{
				
				$local_link_text = $a_query_parameter->get_last_error();
				$local_label_text = $this->lang[self::language_position_label];
				$local_result->add_function_result(
					"","","",t_cuteengine::javascript_instance_name.".".$this->name.".last_tag_name_in_action = ".t_cuteengine::javascript_instance_name.".".$this->name.".current_tag_name_in_action;".
		t_cuteengine::javascript_instance_name.".".$this->name.".current_tag_name_in_action = '';");    
				
				
			}
		}
		// C		
		public function create_tags_structure()
        {
            global $g_javascript_namespace;
            //echo __METHOD__."<BR>";
            parent::create_tags_structure();
			// proprierties
			// C
			$g_javascript_namespace->header_script_generator->add_property_to_a_class(
                get_class($this),'count_tag',"this.name + '_count'",false);
			$g_javascript_namespace->header_script_generator->add_property_to_a_class(
                get_class($this),'current_tag_name_in_action',"''",false);
				
			// L
			$g_javascript_namespace->header_script_generator->add_property_to_a_class(
                get_class($this),'last_tag_name_in_action',"''",false);
			$g_javascript_namespace->header_script_generator->add_property_to_a_class(
                get_class($this),'link_tag',"this.name + '_link'",false);
			$g_javascript_namespace->header_script_generator->add_property_to_a_class(
                get_class($this),'label_tag',"this.name + '_label'",false);
			
			// tags
			// label tag
            $this->label_tag = parent::add_tag(t_base_tag_type::btt_label,
                $this->common_properties_array[t_base_field::common_properties_position_per_field_name]."_label");
			// count tag
            $this->count_tag = parent::add_tag(t_base_tag_type::btt_label,
                $this->common_properties_array[t_base_field::common_properties_position_per_field_name]."_count");
            
            // link tag
            $this->link_tag = parent::add_tag(t_base_tag_type::btt_a,
                $this->common_properties_array[t_base_field::common_properties_position_per_field_name]."_link");
			$this->link_tag->add_property('target','_blank');
            $this->main_tag = $this->parent_part->main_tag;  
			$this->default_start_tag = $this->link_tag;
            //
			

			// event
			
			$this->get_field_event_script(t_base_field::field_event_type_on_insert,"event,41");
            
           // $this->default_result_tag = $local_current_tag;          
            //echo "field name: $this->name - default_start_tag->name: ".$this->default_start_tag->name."<BR>";
            //echo "MAIN FIELD TAG TROVATO";

			
           
           
			
			if (! $this->is_started())
			{
				
				$this->complete_field_tags($this->lang[self::language_position_link],'0',
					$this->lang[self::language_position_label]);
			}
			else
			{
				
			}
			
        }
		// C
		public function create_javascript_initialization_component()
		{
			global $g_javascript_namespace;
			$g_javascript_namespace->header_script_generator->add_method_to_a_class_with_content(
                        get_class($this),'go_to_report',''," 
	window.open(".t_cuteengine::javascript_instance_name.".html_tag(this.link_tag).href);

	") ;
		}
		// F
        public function finalize_creation_field()
        {
			
            global $g_cuteengine;
			parent::finalize_creation_field();
			if ($this->is_started())
			{
				//echo "finalize_creation_field<br>";
				
				$this->link_tag->add_property("href",$this->get_link_value());	
                $local_result = new t_content_loader_result;
				$local_action = $g_cuteengine->actions_list[md5($this->get_action_name('get_report'))];
				$local_action->do_action_from_ajax($local_result);
				if (! $local_result->succeeded)
				{
					throw new Exception('action name: ' . $this->get_action_name('get_report') . ": " . $local_result->error_msg);
				}
				
			}
			
		}	
        // G
        public function get_event_name($a_event_type)
        {

            return /*get_class($this)*/$this->name . "_event_".$a_event_type;
        }
        public function get_event_type($a_ction_name)
        {	
			if ($a_ction_name == $this->get_action_name('get_report_from_event'))
				return t_base_field::field_event_type_on_selected;
			else
				return t_base_field::field_event_type_on_insert;
			
		}
		public function get_language_position_last()
        {
            return self::language_position_last;
        }
		public function get_link_value($a_key_value = '')
        {
			global $g_ambient_namespace;
			if ($a_key_value == '')
				$local_key = $this->get_system_cookie_value();
			else
				$local_key = $a_key_value;			
				
			$local_parameters_list = new t_url_parameters_list(false);
            $local_parameter = $local_parameters_list->add_new_parameter(
                $this->proper_properties_array[self::proper_properties_position_per_component_id_to_get_list],
				t_url_parameter::default_method_label,$local_key);
			$local_url = $g_ambient_namespace->get_absolute_web_file_path(false).$local_parameters_list->compute_disguised_parameters_string();
			//echo "local_url:$local_url<br>";
			return $local_url;
		}
		public function get_result_action_script($a_event_type,$parent_event_type,
            $a_parameter,$a_content_loader_result)
		{
            return '';
        }
        // I
		public function initialize_field_event($a_event_type)
        {			
			global $g_javascript_namespace;
            //global $g_strings_utility_namespace;
           // global $g_url_parameters_namespace;
          //  $local_parameter = $g_url_parameters_namespace->get_parameter_by_component_id($this->parent_component->id);
			$local_event = $this->events[$a_event_type];
			//echo "local_event->event_name: ".$local_event->event_name." - local_event->event_type: $local_event->event_type<br>";
			switch ($local_event->event_type) 
            {
                /***********  t_base_field::field_event_type_on_insert  ***********/                
				case t_base_field::field_event_type_on_selected:
					$g_javascript_namespace->header_script_generator->add_method_to_a_class_with_content(
						get_class($this),$local_event->event_name,'event,a_item_id',"
		var local_argoument = '".t_url_parameter::separator_level_0 . "' + a_item_id + '".t_url_parameter::separator_level_1 . "' + window.cuteengine[this.parent_componet_name].id + ".
		"'".t_url_parameter::separator_level_1 . "' + '';
		".$local_event->body_code."
		");
				break;
				case t_base_field::field_event_type_on_insert:
					$g_javascript_namespace->header_script_generator->add_method_to_a_class_with_content(
						get_class($this),$local_event->event_name,'event,a_item_id',"
		var local_target = event.target ? event.target : event.srcElement;
		this.current_tag_name_in_action = local_target.id;				
		var local_argoument = '".t_url_parameter::separator_level_0 . "' + a_item_id + '".t_url_parameter::separator_level_1 . "' + window.cuteengine[this.parent_componet_name].id + ".
		"'".t_url_parameter::separator_level_1 . "' + '';
		".$local_event->body_code."
		");
				break;

			}	
            
		}		

        // S
		public function set_cookie_value($a_sql)
        {
			$local_key = $this->get_system_cookie_value() ;
            $a_sql->sql_str = str_replace(self::memory_lists_key_param,$local_key,$a_sql->sql_str);
			//echo "a_sql->sql_str: ".$a_sql->sql_str."<br>";
        } 
    }
?>    